<template>
  <div class="wrap">
    <header>
      <div class="logo">
        <img src="/public/logo.png" alt="">
      </div>
      <div class="content">
        <h1>51</h1>
        <span>SHOPPING</span>
      </div>
      <div class="gif">
        <img src="/public/login_gif.gif" alt="">
      </div>
    </header>
    <article>
      <div class="logo">
        <img src="/public/login_bg.png" alt="">
      </div>
      <el-form ref="ruleFormRef" :status-icon="false" :model="ruleForm" status-icon :rules="rules" label-width="70px"
        class="form" hide-required-asterisk>
        <h2>
          登录
        </h2>
        <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" @focus="domState = true" @blur="domState=false" type="text" autocomplete="off" />
            <div class="associate" ref="associate">
              <p v-for="item in history_username" :key="item" @click="clickUsername(item)">{{ item }}</p>
            </div>
        </el-form-item>
        <el-form-item label="密码" prop="pwd1">
          <el-input v-model="ruleForm.pwd1" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="ruleForm.agree" size="large" />
          <a href="/public/policy.html" class="policy">用户使用须知</a>
        </el-form-item>

        <div class="btn">
          <button @click.prevent="doLogin">登录</button>
        </div>
        <router-link class="other" to="/regist">没有账号？点击注册</router-link>
      </el-form>
    </article>
    <footer>
      <div class="info">
        <div class="top">
          <a href="#">关于我们</a>
          <i>|</i>
          <a href="#">联系我们</a>
          <i>|</i>
          <a href="#">人才招聘</a>
          <i>|</i>
          <a href="#">商家入驻</a>
          <i>|</i>
          <a href="#">广告服务</a>
          <i>|</i>
          <a href="#">批发采购</a>
          <i>|</i>
          <a href="#">开放平台</a>
          <i>|</i>
          <a href="#">销售联盟</a>
          <i>|</i>
          <a href="#">帮助中心</a>
        </div>
        <div class="bot">
          Copyright @ 2004-2024 51SHOPPING 版权所有
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import Validate from '@/comm/validate.js'
import { useRouter } from 'vue-router';
import { login } from '@/api/user';
import { useUserStore } from '@/stores/user';
import { setStorage,getStorage } from '@/utils/local.js'
const { ruleForm, rules } = new Validate()
const store = useUserStore()
if (store.uname) {
  ruleForm.username = store.uname
  ruleForm.pwd1 = store.pwd
}
//历史账号
const history_username = ref(JSON.parse(getStorage('username')) || [])
const router = useRouter()
// 表单dom
const ruleFormRef = ref(null)
//联想dom
const associate =ref(undefined)
//控制联想dom高度
const domState = ref(false)
watch(domState,(nva,ova) => {
  if(nva){
    associate.value.style.height = (history_username.value.length * 20) + 'px'
  }else{
    associate.value.style.height = 0
  }
})
//选择账号
function clickUsername(item){
  ruleForm.username = item
  domState.value = false
}
//登录
async function doLogin() {
  await ruleFormRef.value.validate()
  const res = await login({ username: ruleForm.username, password: ruleForm.pwd1 })
  if (res.code === 1) {
    //记住账号
    if(history_username.value.length > 6){
      history_username.value.shift()
    }else{
      history_username.value.push(ruleForm.username)
    }
    setStorage('username', JSON.stringify(Array.from(new Set(history_username.value))))
    store.setUserInfo(res.data)
    router.replace('/')
  } else {
    ElNotification({
      title: '错误',
      message: res.msg,
      type: 'error',
      duration: 2000
    })
  }
}
</script>
<style lang='less' scoped>
.wrap {
  width: 100%;
  height: 100%;

  header {
    // height: 15%;
    height: 109px;
    padding-top: 13px;

    .gif {
      float: right;
      margin: -12px 10% 0 0;
      width: 20%;
      height: 100%;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .logo {
      float: left;
      margin-left: 22%;
      width: 100px;
      height: 104px;

      img {
        width: 100%;
      }
    }

    .content {
      padding: 20px 0 0 10px;
      float: left;
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
      color: #5858db;
      width: 100px;
      height: 104px;

      h1 {
        font-size: 38px;
      }
    }
  }

  article {
    position: relative;
    height: 510px;
    background-color: #5ebced;


    .logo {
      position: absolute;
      left: 20%;
      top: 50%;
      transform: translate(0, -50%);
      width: 396px;
      height: 321px;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .form {
      position: absolute;
      border-radius: 5px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.5);
      right: 15%;
      top: 50%;
      transform: translate(0, -50%);
      padding: 0 33px;
      width: 405px;
      height: 455px;
      background-color: #f2f2f2;


      .el-form-item {
        border: 1px solid #5ebced;
        background-color: #fff;
        border-radius: 15px;

        //联想功能
        .associate {
          position: absolute;
          transition: height .6s;
          height: 0;
          overflow: hidden;
          top: 34px;
          width:100%;
          background-color:#fff;
          z-index: 99;
          p{
            width: 100%;
            height: 20px;
            line-height: 20px;
            &:hover{
              background-color: #26b5ff;
            }
          }
        }
        &:nth-of-type(1){
          position: relative;
        }

        :deep(.el-form-item__content) {
          width: 267px;
          height: 32px;
          border-radius: 15px;
        }

        :deep(.el-input) {
          width: 100%;
          height: 100%;
          border-radius: 15px !important;
          overflow: hidden !important;
        }

        :deep(.el-form-item__label) {
          border-radius: 15px;
          border-top-right-radius: 0;
          border-bottom-right-radius: 0;
          display: flex;
          justify-content: center;
          color: #fff;
          background-color: #26b5ff;
        }

        :deep(.el-input__wrapper) {
          border: none;
          box-shadow: none;
        }

        :deep(.is-focus) {
          box-shadow: none;
        }

        &:nth-of-type(3) {
          background-color: #f2f2f2;
          height: 20px;
          margin-bottom: 0;
          border: none;

          .el-checkbox {
            left: -62px;
            top: -13px;
          }

          &:deep(.el-form-item__error) {
            left: -22%;
            top: 60%;
          }
        }
  
      }

      h2 {
        color: #5ebced;
        height: 50px;
        line-height: 50px;
        text-align: center;
        letter-spacing: 10px;
        margin-bottom: 10px;
      }

      .policy {
        color: #787993;
        font-size: 14px;
        transform: translate(-55px, -13px);
      }

      .btn {
        height: 40px;
        text-align: center;
        margin: 5px;

        button {
          color: #fff;
          width: 40%;
          height: 100%;
          background-color: #26b5ff;
          border-radius: 5px;
        }
      }

      .other {
        padding-left: 5px;
        color: #606266;
        font-size: 14px;

        &:hover {
          color: #26b5ff;
        }
      }
    }
  }

  footer {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 15%;

    i {
      font-style: normal;
      margin: 0 5px;
    }

    .info {
      color: #666666;
      font-size: 12px;

      .top {
        a {
          color: #666666;

          &:hover {
            color: #26b5ff;
            text-decoration: underline;
          }
        }
      }

      .bot {
        text-align: center;
      }
    }
  }
}
</style>